What is create-error-class?
The 'create-error-class' npm package is a utility that allows developers to create custom error classes in JavaScript. This is particularly useful for defining specific types of errors in applications, which can enhance error handling by making it easier to identify and react to different error conditions.
What are create-error-class's main functionalities?
Creating custom error classes
This feature allows developers to create their own error classes. The custom error can then be thrown with a specific message, making it clear what type of error occurred.
const createError = require('create-error-class');
const MyCustomError = createError('MyCustomError');
throw new MyCustomError('Something went wrong');
Other packages similar to create-error-class
http-errors
Similar to 'create-error-class', 'http-errors' is used to create HTTP error objects. While 'create-error-class' is generic and can be used for any type of error, 'http-errors' is specifically tailored for HTTP errors, making it more suitable for web applications.
custom-error-generator
This package also allows the creation of custom error classes. It is quite similar to 'create-error-class' but offers additional options for setting default properties on the error instances, providing slightly more flexibility.
create-error-class
Create error class
Install
$ npm install --save create-error-class
Usage
var createErrorClass = require('create-error-class');
var HTTPError = createErrorClass('HTTPError', function (props) {
this.message = 'Status code is ' + props.statusCode;
});
throw new HTTPError({statusCode: 404});
API
createErrorClass(className, [setup])
Return constructor of Errors with className
.
className
Required
Type: string
Class name of Error Object. Should contain characters, validated by this regex: [^0-9a-zA-Z_$]
.
setup
Type: function
Setup function, that will be called after each Error object is created from constructor with context of Error object.
License
MIT © Vsevolod Strukchinsky